package com.frxuexi.shop.view

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.CommentReqBean
import com.frxuexi.lib_base.entity.Item0
import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.GlideUtils
import com.frxuexi.shop.R
import com.frxuexi.shop.databinding.ActivityCommentBinding
import com.frxuexi.shop.intent.OrderIntent
import com.frxuexi.shop.viewmodel.OrderViewModel
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/shop/CommentActivity")
@AndroidEntryPoint
class CommentActivity : BaseActivity<ActivityCommentBinding,OrderIntent,OrderViewModel>() {
    override val binding: ActivityCommentBinding by lazy { ActivityCommentBinding.inflate(layoutInflater) }
    override val vm: OrderViewModel by viewModels<OrderViewModel>()

    @Autowired
    lateinit var order:Item0
    var shopCore=0f
    var serviceCore=0f
    var path=""

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.UPLOAD->{
                                Log.i("lr",it.data.toString())
                            }
                            else->{
                                ToastUtils.showLong("评论成功")
                                finish()
                            }

                        }

                    }


                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        binding.shopCore.setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
            shopCore=rating
        }

        binding.serviceCore.setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
            serviceCore=rating
        }


        binding.headReply.headAdd.apply {
            setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_ok)
            setOnClickListener {
                val content = binding.repContent.text.toString()
                val reqBean = CommentReqBean(
                    "",
                    content,
                    "19902150000",
                    order.orderId,
                    path,
                    order.orderInfoList.get(0).productId,
                    shopCore.toInt(),
                    serviceCore.toInt(),
                    "",
                    "",
                    877
                )
                vm.sendIntent(OrderIntent.addComment(reqBean))
            }
        }

        binding.repIv.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            path = bean?.path!!
            GlideUtils.loadRoundImage(path,binding.repIv)
//            val split = path!!.split("/")
//            val name = split.get(split.lastIndex)
//            vm.sendIntent(OrderIntent.upload(name,path))
        }
    }

}